package com.zkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkj.dto.DishDTO;
import com.zkj.dto.DishPageQueryDTO;
import com.zkj.entity.Category;
import com.zkj.entity.Dish;
import com.zkj.entity.DishFlavor;
import com.zkj.mapper.CategoryMapper;
import com.zkj.mapper.DishFlavorMapper;
import com.zkj.mapper.DishMapper;
import com.zkj.result.Result;
import com.zkj.service.IDishFlavorService;
import com.zkj.service.IDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkj.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author zkj
 * @since 2024-08-22
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {

    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private IDishFlavorService dishFlavorService;

    // 添加菜品和对应的口味
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            Long dishId = dish.getId();
            flavors.forEach(item -> {
                item.setDishId(dishId);
            });
            dishFlavorService.saveBatch(flavors);
        }
    }

    @Override
    public Result pageListPaging(Integer page, Integer pageSize, String name
            , Integer categoryId, Integer status) {

        DishPageQueryDTO dishPageQueryDTO = new DishPageQueryDTO();
        if (page != null) {
            dishPageQueryDTO.setPage(page);
        }
        if (pageSize != null) {
            dishPageQueryDTO.setPageSize(pageSize);
        }
        if (name != null) {
            dishPageQueryDTO.setName(name);
        }
        if (categoryId != null) {
            dishPageQueryDTO.setCategoryId(Long.valueOf(categoryId));
        }
        if (status != null) {
            dishPageQueryDTO.setStatus(status);
        }

        // 创建分页对象
        Page<DishVO> queryDTOPage = new Page<>(page, pageSize);
        // 使用xml文件中的方法进行分页查询 第一个参数为Page对象，第二个参数为查询条件
        IPage<DishVO> dishVOIPage = dishMapper.queryPageListPaging(queryDTOPage, dishPageQueryDTO);
        return Result.success(dishVOIPage);
    }

    /*
     * 更新菜品状态
     */
    @Override
    public Result updateDishStatus(Integer status, Long id) {
        Dish dish = dishMapper.selectById(id);
        dish.setStatus(status);
        int i = dishMapper.updateById(dish);
        if (i > 0) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @Override
    public Result getDishById(Long id) {
        Dish dish = dishMapper.selectById(id);

        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish, dishDTO);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(queryWrapper);
        dishDTO.setFlavors(dishFlavors);

        return Result.success(dishDTO);
    }

    @Override
    @Transactional
    public Result deleteDish(Long id) {

        dishMapper.deleteById(id);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);

        dishFlavorMapper.delete(queryWrapper);
        return Result.success("删除成功");
    }

    @Override
    public void updateDish(DishDTO dish) {

        Dish dish1 = new Dish();
        BeanUtils.copyProperties(dish, dish1);
        dishMapper.updateById(dish1);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(queryWrapper);

        List<DishFlavor> flavors = dish.getFlavors();
        flavors.forEach(item -> {
            item.setDishId(Long.valueOf(dish.getId()));
        });
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public List<DishVO> listWithFlavor(Long categoryId) {

        List<DishVO> dishVOS = dishMapper.selectListInfo(categoryId);
        return dishVOS;
    }
}
